//
//  LoginView.m
//  01-UIViewHomeWork
//
//  Created by dllo on 16/7/23.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "LoginView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface LoginView ()
@property (nonatomic, strong) UIButton *userButton;
@property (nonatomic, strong) UITextField *userTextField;
@property (nonatomic, strong) UIButton *passwordButton;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *forgetButton;
@property (nonatomic, strong) UIButton *verfButton;
@property (nonatomic, strong) UIButton *seeAnyButton;
@end

@implementation LoginView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatBackView:frame];
        [self creatSubViews:frame];
    }
    return self;
}

- (void)creatBackView:(CGRect)frame {
    UIImageView *backView = [[UIImageView alloc] initWithFrame:self.bounds];
    backView.image = [UIImage imageNamed:@"welcome_2"];
    [self addSubview:backView];
    UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 0.35, HEIGHT * 0.1, WIDTH * 0.3, HEIGHT * 0.2)];
    logoView.image = [UIImage imageNamed:@"logo_lagou"];
    [self addSubview:logoView];
}

- (void)creatSubViews:(CGRect)frame {
    self.userButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _userButton.alpha = 0.5;
    _userButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.12 blue:0.11 alpha:1.00];
    [_userButton setImage:[UIImage imageNamed:@"icon_username"] forState:UIControlStateNormal];
    _userButton.frame = CGRectMake(20, HEIGHT * 0.35, 50, 50);
    [_userButton addTarget:self action:@selector(userButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_userButton];
    
    self.userTextField = [[UITextField alloc] initWithFrame:CGRectMake(_userButton.frame.origin.x + _userButton.frame.size.width, _userButton.frame.origin.y, WIDTH - 40 - 50, 50)];
    _userTextField.backgroundColor = [UIColor colorWithRed:0.30 green:0.28 blue:0.28 alpha:1.00];
    _userTextField.alpha = 0.8;
    [self addSubview:_userTextField];
    
    self.passwordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _passwordButton.alpha = 0.5;
    _passwordButton.backgroundColor = _userButton.backgroundColor;
    [_passwordButton setImage:[UIImage imageNamed:@"icon_password"] forState:UIControlStateNormal];
    _passwordButton.frame = CGRectMake(_userButton.frame.origin.x, 2 + _userButton.frame.origin.y + _userButton.frame.size.height, _userButton.frame.size.width, _userButton.frame.size.height);
    [_passwordButton addTarget:self action:@selector(pwdButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_passwordButton];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(_userTextField.frame.origin.x, _passwordButton.frame.origin.y, _userTextField.frame.size.width, _userTextField.frame.size.height)];
    _passwordTextField.backgroundColor = _userTextField.backgroundColor;
    [self addSubview:_passwordTextField];
    
    self.verfButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.verfButton.frame = CGRectMake(_passwordTextField.frame.origin.x + _passwordTextField.frame.size.width * 0.6, _passwordTextField.frame.origin.y, _passwordTextField.frame.size.width * 0.4, _passwordTextField.frame.size.height);
    [self.verfButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_verfButton setTitleColor:[UIColor colorWithRed:0.12 green:0.60 blue:0.49 alpha:1.00] forState:UIControlStateNormal];
    [self addSubview:_verfButton];
    
    self.forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _forgetButton.frame = CGRectMake(WIDTH * 0.6, HEIGHT * 0.5, 120, 30);
    [_forgetButton setImage:[UIImage imageNamed:@"icon_wenhao"] forState:UIControlStateNormal];
    [_forgetButton addTarget:self action:@selector(forgetButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_forgetButton];
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginButton.frame = CGRectMake(_userButton.frame.origin.x, HEIGHT * 0.61, _userButton.frame.size.width + _userTextField.frame.size.width, _userTextField.frame.size.height);
    _loginButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.71 blue:0.55 alpha:1.00];
    [_loginButton addTarget:self action:@selector(loginButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_loginButton];
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.frame = CGRectMake(_loginButton.frame.origin.x, _loginButton.frame.origin.y + _loginButton.frame.size.height + 20, _loginButton.frame.size.width, _loginButton.frame.size.height);
    _registerButton.backgroundColor = _loginButton.backgroundColor;
    _registerButton.alpha = 0.8;
    [_registerButton addTarget:self action:@selector(regsisterButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_registerButton];
    
    self.seeAnyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _seeAnyButton.frame = CGRectMake(WIDTH * 0.8, 20, WIDTH * 0.2, 20);
    [_seeAnyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_seeAnyButton setTitle:@"随便逛逛" forState:UIControlStateNormal];
    [self addSubview:_seeAnyButton];
    [_seeAnyButton addTarget:self action:@selector(jumpToTabBar:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)regsisterButtonDidClicked:(UIButton *)button {
    [self.delegate jumpViewController:button];
}

- (void)loginButtonDidClicked:(UIButton *)button {
    
}

- (void)forgetButtonDidClicked:(UIButton *)button {
    [self.delegate jumpViewController:button];
}

- (void)userButtonDidClicked:(UIButton *)button {
    [_userTextField becomeFirstResponder];
    button.selected = YES;
    _passwordButton.selected = NO;
}

- (void)pwdButtonDidClicked:(UIButton *)button {
    [_passwordTextField becomeFirstResponder];
    button.selected = YES;
    _userButton.selected = NO;
}

- (void)jumpToTabBar:(UIButton *)button {
    [self.delegate jumpViewController:button];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

@end
